package testGUI;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import de.szut.bomberman.graphics.gamefield.Bomb;

public class Listener extends KeyAdapter {

	private Move movementThreadPlayerOne;
	private Move movementThreadPlayerTwo;
	private boolean playerOneThreadIsRunning;
	private boolean playerTwoThreadIsRunning;

	private JLabel playerOne;
	private JLabel playerTwo;

	private JLayeredPane contentPane;

	public Listener(JLabel player1, JLabel player2,
			ArrayList<Point> stonePositions, JLayeredPane contentPane) {
		playerOne = player1;
		playerTwo = player2;
		movementThreadPlayerOne = new Move(player1, stonePositions);
		movementThreadPlayerTwo = new Move(player2, stonePositions);
		movementThreadPlayerOne.start();
		movementThreadPlayerTwo.start();
		playerOneThreadIsRunning = false;
		playerTwoThreadIsRunning = false;
		this.contentPane = contentPane;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			movementThreadPlayerOne
					.setMovementDirection(Direction.RIGHT.name());
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			movementThreadPlayerOne.setMovementDirection(Direction.LEFT.name());
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_UP) {
			movementThreadPlayerOne.setMovementDirection(Direction.UP.name());
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			movementThreadPlayerOne.setMovementDirection(Direction.DOWN.name());
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			movementThreadPlayerTwo
					.setMovementDirection(Direction.RIGHT.name());
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			movementThreadPlayerTwo.setMovementDirection(Direction.LEFT.name());
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_W) {
			movementThreadPlayerTwo.setMovementDirection(Direction.UP.name());
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			movementThreadPlayerTwo.setMovementDirection(Direction.DOWN.name());
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
			JLabel bomb = new JLabel(new ImageIcon(new Bomb().getTexture()));
			bomb.setSize(50, 50);
			bomb.setLocation(playerOne.getLocation());
			contentPane.add(bomb, new Integer(2));
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_UP) {
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_W) {
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
